//
//  SimpleTableViewController.m
//  SimpleTable
//
//  Created by Simon on 1/12/13.
//  Copyright (c) 2013 Appcoda. All rights reserved.
//

#import "SimpleTableViewController.h"
#import "Scores.h"
#import <sqlite3.h>

@interface SimpleTableViewController ()

@end

@implementation SimpleTableViewController
{
    NSMutableArray* scores;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated {
    [self getScore];
}

-(void) getScore{
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"score"];
    if (dataRepresentingSavedArray != nil)
    {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
        if (oldSavedArray != nil)
            scores = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            scores = [[NSMutableArray alloc] init];
    }
    
    NSLog(@"%i", [scores count]);
    
    NSSortDescriptor * sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
    [scores sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];
    
    
//    scores = (NSMutableArray *)sortedArray;
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [scores count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    Scores*s = scores[indexPath.row];
    UILabel *lblName = (UILabel *)[cell viewWithTag:100];
    [lblName setText:s.namePlayer];
    UILabel *lblName1 = (UILabel *)[cell viewWithTag:200];
    [lblName1 setText:[NSString stringWithFormat:@"%d",s.score]];
    if(indexPath.row == 0)
    {
        cell.imageView.image = [UIImage imageNamed:@"cup_gold.png"];
    }
    else if(indexPath.row == 1)
    {
        cell.imageView.image = [UIImage imageNamed:@"cup_silver.png"];
    }
    else if(indexPath.row == 2)
    {
        cell.imageView.image = [UIImage imageNamed:@"cup_bronze.png"];
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"ic_launcher.png"];
    }
    
    return cell;
    
}

- (void) findContact:(id)sender
{
    const char *dbpath = [_databasePath UTF8String];
    sqlite3_stmt    *statement;
    
    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:
                              @"SELECT name, score FROM scores"];
        
        const char *query_stmt = [querySQL UTF8String];
        
        if (sqlite3_prepare_v2(_contactDB,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                /*NSString *addressField = [[NSString alloc]
                                          initWithUTF8String:
                                          (const char *) sqlite3_column_text(
                                                                             statement, 0)];*/
                //_address.text = addressField;
                /*NSString *phoneField = [[NSString alloc]
                                        initWithUTF8String:(const char *)
                                        sqlite3_column_text(statement, 1)];*/
                //_phone.text = phoneField;
                NSLog(@"Match found");
            } else {
                NSLog(@"Match not found");
                //_address.text = @"";
                //_phone.text = @"";
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(_contactDB);
    }
}
@end
